var controller = {};
var base64 = require('js-base64').Base64;

function reCodeResult(targets){
    var result = targets;
    for(var i = 0; i < result.length; i++){
        result[i].content = base64.decode(result[i].content)
    }
    console.log(result)
    return result;
}

function reCodeResultMobile(targets){
    var result = targets;
    for(var i = 0; i < result.length; i++){
        result[i].content = removeHTMLTag(base64.decode(result[i].content)).substring(0, 80) + '...'
    }
    console.log(result)
    return result;
}

function removeHTMLTag(str) {
    str = str.replace(/<\/?[^>]*>/g,''); //去除HTML tag
    str = str.replace(/[ | ]*\n/g,'\n'); //去除行尾空白
    //str = str.replace(/\n[\s| | ]*\r/g,'\n'); //去除多余空行
    str=str.replace(/&nbsp;/ig,'');//去掉&nbsp;
    return str;
}

controller.savePreview = function(req, res, next){
    var ql = 'insert into content(id,tel,firstnav,secondnav,time,pw,content) values ("0","' + + global.USER_ACCOUNT + '","' + req.query.firstnav + '","' + req.query.secondnav + '","' + req.query.time + '","000000","");'
    console.log(ql);
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

controller.renamePreview = function(req, res, next){
    var ql = 'update content set secondnav = "' + req.query.newName + '", time="' + req.query.time + '" where firstnav="' + req.query.firstnav + '" and secondnav ="' + req.query.oldName + '" and tel ="' + global.USER_ACCOUNT + '";';
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

controller.getPreview = function(req, res, next){
    var ql = 'select id, tel, firstnav, secondnav, time, content, filename from content where tel = "' + global.USER_ACCOUNT + '" and firstnav = "' + req.query.account + '" and id != "noFile" and id != "deFolder" and id!="deFile"';
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;

        res.send({
            code: 200,
            previewList: reCodeResult(results),
            account: global.USER_ACCOUNT
        })
    });
}

controller.deletePreview = function(req, res, next){
    if ( req.query.previewId === 'upload') {
    var ql = 'update content set id = "deFile", time ="'+ req.query.time +'" where firstnav ="' + req.query.firstNav + '" and secondnav ="' + req.query.name +'" and tel ="' + global.USER_ACCOUNT + '";';
    } else {
        var ql = 'update content set id = "deFolder", time ="'+ req.query.time +'" where firstnav ="' + req.query.firstNav + '" and secondnav ="' + req.query.name +'" and tel ="' + global.USER_ACCOUNT + '";';
    }
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;
        res.send({
            code: 200
        })
    })
}

controller.getAllPreview = function(req, res, next){
    var ql = 'select id, tel, firstnav, secondnav, time, content_id, content, filename from content where id != "noFile" and id != "deFolder" and id!="deFile"';
    console.log(ql)
    conn.query(ql, function(err, results, fields){
        if(err) throw err;

        res.send({
            code: 200,
            previewList: reCodeResultMobile(results),
            account: global.USER_ACCOUNT
        })
    });
}

module.exports = controller;